package cloud.authentication.server.authentication.service.impl;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import cloud.authentication.server.authentication.service.IAuthenticationService;
import cloud.authentication.server.po.Resource;

@Service
@Slf4j
public class AuthenticationService implements IAuthenticationService {

	/**
     * 未在资源库中的URL默认标识
     */
    public static final String NONEXISTENT_URL = "NONEXISTENT_URL";

    @Autowired
    private ResourceService resourceService;
    
    /**
     * @param authRequest 访问的url,method
     * @return 有权限true, 无权限或全局资源中未找到请求url返回否
     */
	@Override
	public boolean decide(HttpServletRequest authRequest) {
		log.debug("正在访问的url是:{},method:{}",authRequest.getServletPath(),authRequest.getMethod());
		//获取用户认证信息
		Authentication authentication =SecurityContextHolder.getContext().getAuthentication();
		//获取此url，method访问对应的权限资源信息
		ConfigAttribute urlConfigAttribute = resourceService.findConfigAttributesByUrl(authRequest);
		if (NONEXISTENT_URL.equals(urlConfigAttribute.getAttribute())) {
			log.debug("url未在资源池中找到，拒绝访问");
			
		}
		return false;
	}
    
    
    
	
}
